package com.buffer.agent.tool;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@Component
public class WeatherTool implements AgentTool {
    @Override
    public ToolType getType() {
        return ToolType.LOCAL_WEATHER;
    }
    
    @Override
    public ToolResult execute(String param) {
        try {
            // 默认城市为北京
            String city = "北京";
            if (param != null && !param.trim().isEmpty()) {
                city = param.trim();
            }
            
            // 模拟天气数据，避免外部API调用问题
            String[] weatherConditions = {"晴天", "多云", "阴天", "小雨", "中雨", "大雨", "雪天", "雾天"};
            Random random = new Random();
            String weather = weatherConditions[random.nextInt(weatherConditions.length)];
            int temperature = random.nextInt(35) - 10; // -10到25度
            
            String currentTime = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            
            String result = String.format(
                "天气查询结果:\n城市: %s\n天气状况: %s\n温度: %d°C\n查询时间: %s", 
                city, weather, temperature, currentTime
            );
            
            return new ToolResult(true, result);
        } catch (Exception e) {
            return new ToolResult(false, "天气查询失败: " + e.getMessage());
        }
    }
}